<?php
//模块名_控制名
class Product_PutawayController extends Zend_Controller_Action
{
	public function preDispatch(){
		$this->tplDirectory = "product/putaway/";
	}

	public function receivingAction(){
		echo Cff::renderTpl($this->tplDirectory.'putaway.tpl',"layout");
	}
	
	/**
	*上架查询
	*/
	public function searchPutawayAction(){
		if($this->_request->isPost()){
			$putaway_code = trim($this->_request->getParam("putawayNo",""));
			$receiving_code = trim($this->_request->getParam("receivingNo",""));
			$dateFrom = trim($this->_request->getParam("dateFrom",""));
			$putaway_status = trim($this->_request->getParam("status",""));
			$dateTo = trim($this->_request->getParam("dateTo",""));
			$page = trim($this->_request->getParam("page",1));
			$pageSize = trim($this->_request->getParam("pageSize",20));
			$page=($page)<1?1:$page;

			$condition = array(
					"receiving_code" => $receiving_code,
					"putaway_code" => $putaway_code,
					"putaway_date_from" => $dateFrom,
					"putaway_date_to" => $dateTo,
					'putaway_status'=>$putaway_status,
				);
				
			$total = Product_Service_Putaway::getByCondition($condition,"count(*)");
			$return = array("ask" =>0,"total" => $total);
			if($total>0){
				$list = Product_Service_Putaway::getByCondition($condition,"*",$page,$pageSize);
				if(!empty($list)){
					foreach($list as $key => $val){
						$row=Product_Service_Product::getByProductId($val['product_id']);
						$list[$key]['product_sku']=$row['product_sku'];
						$list[$key]['customer_id']=$row['customer_id'];
						$usurow=User_Service_User::getByUserId($val['putaway_user_id']);
						$list[$key]['putaway_user_id']=$usurow['name'];
						$list[$key]['putaway_status']=Product_Service_Putaway::$putaway_status[$val['putaway_status']];
					}
					$return['ask'] = 1;
					$return['total'] = $total;
					$return['result'] = $list;
				}
			}
			die(json_encode($return));
		}
		$this->view->status=Product_Service_Putaway::$putaway_status;
		echo Cff::renderTpl($this->tplDirectory.'search-putaway.tpl',"layout");
	}
	
	public function receivingListAction(){
		$receivingCode = trim($this->_request->getParam("receivingCode",""));
		$putawayNo = trim($this->_request->getParam("putawayno",""));
		if($receivingCode==''&& $putawayNo=='' ){
			die(json_encode(array('ask'=>false)));
		}
		$condition = array(
				"receiving_code" => $receivingCode,
		);
		$psrRow=Product_Service_Receiving::getByReceivingCode($receivingCode);
		if(empty($psrRow)){
			die(json_encode(array('ask'=>false)));
		}elseif($psrRow['status']!='3' && $psrRow['status']!='2'  ){
			die(json_encode(array('ask'=>false,'error'=>$receivingCode.' 未完成收货!')));
		}
		$list = Product_Service_Receiving::getByReceivingItemCondition($condition);
		foreach($list as $key => $value){
			if($value['return_result']==0){//状态为销毁，不能上架
				$list[$key]['wait_putaway_total']=0;
			}else{
				$list[$key]['wait_putaway_total']=$value['received_qty']-$value['putaway_qty'];			//等待分配上架数量
			}
			$rows=Product_Service_Product::getByProductId($value['product_id']);
			$list[$key]['product_sku']=$rows['product_sku'];
			$list[$key]['product_title']=$rows['product_title'];
		}
		$return = array("ask" => false);
		if(!empty($list)){
			$return['ask'] = true;
			$return['result'] = $list;
		}
		die(json_encode($return));
	}
	
	public function putawayListAction(){
		$putawayNo = trim($this->_request->getParam("putawayno",""));
		if($putawayNo==''){
			die(json_encode(array('ask'=>false)));
		}
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->user_id;					//操作人
		$contion=array(
			'putaway_code'=>$putawayNo,
		);
		$rows = Product_Service_Putaway::getByCondition($contion,"*");
		if($rows){
			foreach($rows as $key => $val){
				$row=Product_Service_Product::getByProductId($val['product_id']);
				$rows[$key]['product_sku']=$row['product_sku'];
				$wsprows=Warehouse_Service_Products::getByProductId($val['product_id']);//仓储查询
				$rows[$key]['default_ws_code']=$wsprows['ws_code'];//货架号
				$pspRow=Product_Service_Product::getProductCategory($val['product_id']);
				$rows[$key]["category"]=$pspRow['poc_value'];
				$rows[$key]["customer_id"]=$row['customer_id'];
				$rows[$key]['putaway_status_text']=Product_Service_Putaway::$putaway_status[$val['putaway_status']];
			}
			$result=array(
				'ask'=>true,
				'result'=>$rows,
			);
		}else{
			$result=array(
				'ask'=>false,
			);
		}
		die(json_encode($result));
	}
	
	public function putawayInfoAction(){
		$ri_id = trim($this->_request->getParam("ri_id",""));
		if($ri_id==''){
			die(json_encode(array('ask'=>false)));
		}
		$rows=Product_Service_ReceivingItem::getByReceivingId($ri_id);
		if($rows){
			$wsprows=Warehouse_Service_Products::getByProductId($rows['product_id']);//仓储查询
			$rows['default_ws_code']=$wsprows['ws_code'];//货架号
			$rows['wait_putaway_total']=$rows['received_qty']-$rows['putaway_qty']; //等待分配上架的总数
			
			$psprows=Product_Service_Product::getByProductId($rows['product_id']);
			$rows['product_sku']=$psprows['product_sku'];
			$pspRow=Product_Service_Product::getProductCategory($rows['product_id']);
			$rows["category"]=$pspRow['poc_value'];
			$rows["customer_id"]=$psprows['customer_id'];
			$rows["putaway_ids"]='0';
			
			$Receiving=Product_Service_Receiving::getByReceivingCode($rows['receiving_code']);

			if(isset($putawayRows)){
				$data=$putawayRows;
			}else{
				$data=array($rows);
			}

			$result=array(
				'ask'=>1,
				'result'=>$data
			);
			die(json_encode($result));
		}
	}
	/**
	*查看库存
	*/
	public function inventoryAction(){
		$product_id = trim($this->_request->getParam("product_id",""));
		if($product_id==''){
			die(json_encode(array('ask'=>false)));
		}
		$rows=Warehouse_Service_Products::getByAllProductId($product_id);
		$psprows=Product_Service_Product::getByProductId($product_id);
		foreach($rows as $key => $value){
			$rows[$key]['product_sku']=$psprows['product_sku'];
			$wsRow=Warehouse_Service_Shelf::getByfield('ws_code',$value['ws_code']);
			$rows[$key]['wsc_code']=$wsRow['wsc_code'];
		}
		if($rows){
			$result=array(
				'ask'=>1,
				'result'=>$rows
			);
		}else{
			$result=array(
				'ask'=>0,
				'result'=>array('error'=>array('库存无此产品!')),
			);
		}
		die(json_encode($result));
	}
	/**
	*上架
	*/
	public function submitPutawayAction(){
		$params = $this->_request->getParams();
		if(!$this->_request->isPost()){
			die(json_encode(array('ask'=>0,'result'=>array('error'=>array('操作失败!')))));
		}
		if(!isset($params['received_qty'])){
			die(json_encode(array('ask'=>0,'result'=>array('error'=>array('操作失败,没有数据!')))));
		}
		
		$rows=array();
		foreach($params as $key => $param){	
			$rows[$key] = $param;
		}
		$data=array(
			'quantity'=>$rows['received_qty'],
			'ws_code'=>$rows['ws_code'],
			'putaway_lot_number'=>$rows['lot'],
			'product_id'=>$rows['product_id'],
			'product_sku'=>$rows['product_sku'],
			'receiving_code'=>$rows['receiving_code'][0],
		);
		if(isset($rows['putaway_ids'])){
			$data['putaway_ids']=$rows['putaway_ids'];
		}
		//数据验证
		$error=Product_Service_Putaway::validate($data,0);
		if(!empty($error)){
			die(json_encode(array('ask'=>0,'result'=>array('error'=>$error))));
		}
		//创建
		$result=Product_Service_Putaway::proccreatePutawayTransaction($data);
		if($result==false){
			die(json_encode(array('ask'=>0,'result'=>array('error'=>array('操作失败!')))));
		}else{
			die(json_encode(array('ask'=>1,'result'=>array('msg'=>"成功创建上架清单:".$result,'putaway_code'=>$result))));
		}

	}

	/**
	*确认上架、修改
	*/
	public function submitEditPutawayAction(){
		$params = $this->_request->getParams();
		if(!$this->_request->isPost()){
			die(json_encode(array('ask'=>0,'result'=>array('error'=>array('操作失败!')))));
		}
		$rows=array();
		foreach($params as $key => $param){	
			$rows[$key] = $param;
		}
		$data=array(
			'quantity'=>$rows['putaway_quantity'],
			'ws_code'=>$rows['putaway_ws_code'],
			'putaway_lot_number'=>$rows['putaway_lot_number'],
			'product_id'=>$rows['product_id'],
			'product_sku'=>$rows['product_sku'],
			'putaway_id'=>$rows['py_ids'],
		);
		//表单数量验证
		$error=Product_Service_Putaway::validate($data,1);
		if(!empty($error)){
			die(json_encode(array('ask'=>0,'result'=>array('error'=>$error))));
		}
		//整理数组
		$pspRow=Product_Service_Putaway::getByPutawayId($data['putaway_id'][0]);
		$receiving_code=$pspRow['receiving_code'];
		$putaway_code=$pspRow['putaway_code'];
		$data['receiving_code']=$receiving_code;
		$data['putaway_code']=$putaway_code;
        //事务处理
		$result = Product_Service_Putaway::procPutawayTransaction($data);
        if(isset($result['ask']) && $result['ask']=='1' ){
            die(json_encode(array('ask'=>1,'result'=>array('msg'=>"已完成,上架清单:".$result['putaway_code'],'putaway_code'=>$result['putaway_code']))));
        }else{
			die(json_encode(array('ask'=>0,'result'=>array('error'=>array('操作失败:'.$result['error'])))));
		}
	}
	/**
	*打印上架清单
	*/
	public function printPutawaynoAction(){
		$putaway_code = $this->_request->getParam('putawayno','');
		if($putaway_code==''){
			die("操作失败!");
		}
		$session = new Zend_Session_Namespace('userAuth');
		$user_name=$session->name;					//操作人
		$contion=array(
			'putaway_code'=>$putaway_code,
		);
		$result = Product_Service_Putaway::getByCondition($contion,"*");
		foreach($result as $key => $val){
			$rows=Product_Service_Product::getByProductId($val['product_id']);
			$result[$key]['product_sku']=$rows['product_sku'];
		}
		$receiving_code=$result[0]['receiving_code'];
		$msrRow=Product_Service_Receiving::getByReceivingCode($receiving_code);
		$rows=array(
			'customer_id'=>$msrRow['customer_id'],
			'receiving_code'=>$receiving_code,
			'putaway_code'=>$putaway_code,
			'name'=>$user_name,
			'date'=>date('Y-m-d H:i:s'),
			'description'=>$msrRow['description'],
		);
		$this->view->info = $rows;
		$this->view->result = $result;
		echo Cff::renderTpl($this->tplDirectory.'/print-putaway.tpl',"layoutheader");
	}
	/**
	*条形码
	*/
	public function barcodeAction(){
		Product_Service_Barcode::barcode($this->_request->code);
	}
	/**
	*获取货架号
	*/
	public function getShelfAction(){
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize',60);
		$s_wa_code=$this->_request->getParam('s_wa_code','');
		$s_wh_code=$this->_request->getParam('s_wh_code','');
		$s_wa_type=$this->_request->getParam('s_wa_type','1');//应用类型
		$s_warehouse_id=$this->_request->getParam('s_warehouse_id','');
		$product_id=trim($this->_request->getParam('select_product_id','0'));
		if($product_id==''||$product_id==0){
			die(Zend_Json::encode(array('ask'=>0,'total'=>0)));
		}
		$condition=array(
			'wa_code'=>$s_wa_code,
			'wh_code'=>$s_wh_code,
			'wa_type'=>$s_wa_type,
			'warehouse_id'=>$s_warehouse_id,
			'product_id'=>$product_id,
		);
		$total=Warehouse_Service_Shelf::getByJoinChooseCondition($condition,'count(*)','',0,0);
		$data=array(
			'total'=>0,
			'ask'=>0
			);
		if($total>0){
			$intpage=($intpage<1)?1:$intpage;
			$Rows=Warehouse_Service_Shelf::getByJoinChooseCondition($condition,'*','',$pageSize,$intpage);
			foreach($Rows as $key => $val){
				$result[$key]['ws_code']=$val['ws_code'];
			}
			unset($Rows);
			$data=array(
				'total'=>$total,
				'result'=>$result,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
	}
}